package com.example.saifly.inPageMy;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.common.Activity;
import com.example.common.Application.MyApplication;
import com.example.common.Retrofit.RetrofitHelper;
import com.example.saifly.Model.AdviceEquipment;
import com.example.saifly.Model.AdviceExercise;
import com.example.saifly.Model.AdviceMedicine;
import com.example.saifly.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

import static com.example.common.Util.TimeUtil.Date2StringYMD;

public class DoctorAdviceActivity extends Activity {

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new Runnable() {
            @Override
            public void run() {
                MedicineExerciseEquipment(sp.getString("uid","12345678910"));

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myAdapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();

    }

    RecyclerView rvDoctorAdvice;
    List<Item> itemList;
    MyAdapter myAdapter;

    private ImageView ivReturn;
    private SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);

    private List<AdviceMedicine> adviceMedicineList;    //每日计划的list
    private List<AdviceExercise> adviceExerciseList;
    private List<AdviceEquipment> adviceEquipmentList;

    private class Item implements Comparable<Item>{
        String begin;
        String text;
        String end;

        public Item(String begin, String text, String end) {
            this.begin = begin;
            this.text = text;
            this.end = end;
        }

        public String getBegin() {
            return begin;
        }

        public void setBegin(String begin) {
            this.begin = begin;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getEnd() {
            return end;
        }

        public void setEnd(String end) {
            this.end = end;
        }

        @Override
        public int compareTo(Item o) {
            return this.getBegin().compareTo(o.getBegin());
        }
    }
    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_my_mydoctor_advice;
    }

    @Override
    protected void init() {
        super.init();

        initList();

        rvDoctorAdvice=findViewById(R.id.mydocter_advice_rv);

        RecyclerView.LayoutManager layoutManager=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
        rvDoctorAdvice.setLayoutManager(layoutManager);

        myAdapter=new MyAdapter();
        rvDoctorAdvice.setAdapter(myAdapter);

        ivReturn = findViewById(R.id.home_doctorAdvice_return);
        doJump();

    }

    private void doJump() {
        ivReturn.setOnClickListener(view -> finish());
    }

    private void initList(){
        itemList=new ArrayList<>();

    }
    private class MyAdapter extends BaseQuickAdapter<Item, BaseViewHolder> {
        public MyAdapter() {
            super(R.layout.holder_mydocter_advice, itemList);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, Item item) {
            final int position=holder.getLayoutPosition();

            holder.setText(R.id.holder_mydocter_begin,itemList.get(position).begin);
            holder.setText(R.id.holder_mydocter_text,itemList.get(position).text);
            holder.setText(R.id.holder_mydocter_end,itemList.get(position).end);
        }
    }

    private interface ApiDoctorAdvice
    {
        @Headers({"Content-Type:application/json"})                             //大家都不变
        @POST("/arrange/getDoctorAdvice")                                    //后端给的路径
        Call<ResponseBody> MedicineExerciseEquipmentInfo(@Body JSONObject jsonObject);
    }

    private void MedicineExerciseEquipment(String patientPhone){
        JSONObject jsonObject = new JSONObject();

        RetrofitHelper retrofitHelper=new RetrofitHelper();     //大家都一样
        Retrofit retrofit=retrofitHelper.getRetrofit();

        ApiDoctorAdvice apiGetDailyPlan = retrofit.create(ApiDoctorAdvice.class);
        jsonObject.put("patientPhone",patientPhone);

        Call<ResponseBody> call = apiGetDailyPlan.MedicineExerciseEquipmentInfo(jsonObject);
        final Response<ResponseBody> response;
        try {
            response = call.execute();
            String result = JSON.parseObject(response.body().string()).get("result").toString();
            Log.d("123", "MedicineExerciseEquipment: "+result);
            adviceMedicineList = JSONObject.parseArray(JSON.parseObject(result).get("medicineInfomations").toString(),AdviceMedicine.class);
            adviceExerciseList = JSONObject.parseArray(JSON.parseObject(result).get("exerciseInfomations").toString(),AdviceExercise.class);
            adviceEquipmentList = JSONObject.parseArray(JSON.parseObject(result).get("treatmentEquipments").toString(),AdviceEquipment.class);
            for(int i = 0; i < adviceMedicineList.size(); i++){
                itemList.add(new Item(Date2StringYMD(adviceMedicineList.get(i).getStartTime()),adviceMedicineList.get(i).getName(),Date2StringYMD(adviceMedicineList.get(i).getEndTime())));
            }
            for(int i = 0; i < adviceExerciseList.size(); i++){
                itemList.add(new Item(Date2StringYMD(adviceExerciseList.get(i).getStartTime()),adviceExerciseList.get(i).getName(),Date2StringYMD(adviceExerciseList.get(i).getEndTime())));
            }
            for(int i = 0; i < adviceEquipmentList.size(); i++){
                itemList.add(new Item(Date2StringYMD(adviceEquipmentList.get(i).getStartTime()),adviceEquipmentList.get(i).getName(),Date2StringYMD(adviceEquipmentList.get(i).getEndTime())));
            }
            Collections.sort(itemList);
            for(int i = 0; i < itemList.size(); i++){
                Log.d("123", "MedicineExerciseEquipment: "+itemList.get(i).getBegin());
                Log.d("123", "MedicineExerciseEquipment: "+itemList.get(i).getText());
                Log.d("123", "MedicineExerciseEquipment: "+itemList.get(i).getEnd());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
